package C2DMRelated;


import ChatPackage.ChatHandler;
import Database.Database;
import TomcatRelated.TomcatProtocolImp;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


public class MessageHandler
{

	//***************************************Class Variables*******************************************//


	private final String tag = new String("MessageHandler");


	//***************************************Constructors*******************************************//


	public MessageHandler()
	{
	}


	//***************************************Public Methods*******************************************//


	//TODO - check if finished!
	public void handleMessage(Context context, Intent intent) 
	{
		Log.v(tag,"handling message.........");

		String messageString = intent.getExtras().getString("message");

		C2DMMessage newC2DMMessage = new C2DMMessage(messageString);
		if(newC2DMMessage.getType().equals(C2DMMessage.MESSAGE_WAITING))
		{
			recieveMessageFromServer(context, newC2DMMessage);
		}
		else
		{
			Toast.makeText(context, "unknown C2DM message was recieved: " + messageString, Toast.LENGTH_LONG).show();
			Log.v(tag,"unknown C2DM message was recieved: " + messageString);			
		}
	}


	//***************************************Private Methods*******************************************//


	private void recieveMessageFromServer(Context context, C2DMMessage newC2DMMessage) 
	{
		Log.v(tag, "handling 'waiting_message'....");

		//getting the device's unique id
		String my_android_id = Database.getInstance(context).get_device_id();

		if(my_android_id == null)
		{
			Log.v(tag, "my_android_id is null! something is wrong! 'MessageHandler.java'");
			Toast.makeText(context, "my_android_id is null! something is wrong! " +
					"'MessageHandler.java'", Toast.LENGTH_LONG).show();
			return;
		}

		String message_id = String.valueOf(newC2DMMessage.getParamValue("message_id"));
 
		//the message from the server (we have to take out the 'from' string and the body of the message)
		String message = TomcatProtocolImp.getInstance().recieveTextMessage(my_android_id, message_id);

		//taking out the 'from' variable
		int indexOfDelimiter = message.indexOf('_');
		if(indexOfDelimiter == -1)
		{
			Log.v(tag, "Error was accured: should have been delimiter '_'");
			return;
		}

		int lengthOfWholeMessage = message.length();
		if(indexOfDelimiter + 1 >  lengthOfWholeMessage)
		{
			Log.v(tag, "message was empty");
			return;
		}

		String sent_from = message.substring(0, indexOfDelimiter);
		String message_content = message.substring(indexOfDelimiter + 1, lengthOfWholeMessage);
		
		//TODO get the sender UserProfile
		ChatHandler.handleChatMessage(context,sent_from, message_content);
		
		
		//Toast.makeText(currentCon, "message was recieved!!!!: '" + message_content + "'", Toast.LENGTH_LONG).show();
		
		
		Log.v(tag,"message was recieved: " + message_content);

	}
}
